package com.sunfeng.netty.atguigu2.nio;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


/**
 * @author sunfeng
 * @date 2021/03/22 16:15
 **/
public class NIOFileChannel02 {
    public static void main(String[] args) throws IOException {


        File file = new File("d:\\file01.txt");

        //创建一个输出流
        FileInputStream fileInputStream = new FileInputStream(file);
        // 根据输出流获取一个文件fileChannel
        // fileChannel 的真实类型是fileChannelImpl
        FileChannel fileChannel = fileInputStream.getChannel();
        //创建一个缓冲区ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());


        // 将byteBuffer 数据
        fileChannel.read(byteBuffer);

        // 将缓冲区的字节转换成字符串
        System.out.println(new String(byteBuffer.array()));





    }
}
